/**
 * 
 */
package com.dwa.persistent.dao;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;

import com.dwa.domain.INews;
import com.dwa.persistent.AbstractDao;
import com.dwa.persistent.PersistentException;


/**
 * @author Emperor
 * 
 */
public class NewsDao extends AbstractDao implements INewsDao 
{
	
	private SimpleJdbcInsert jdbcInsert = null;
	
	interface CONST {
		interface QUERY {

			String GET_NEWS_QUEUEY = "SELECT * FROM NEWS";
			
			String GET_NEWS_BY_NEWS_DATE =  "select "+
			" id,"  +
			" title,"+ 
			" description," +
			" created_on," +
			" show_flag," +
			" news_date"+
			" from " +
			" news" +
			" where DATE_FORMAT(news_date,'%Y%m%d') = DATE_FORMAT(now(),'%Y%m%d')" +
			" limit 0, 50";
			
			String GET_NEWS =  "select "+
			" id,"  +
			" title,"+ 
			" description," +
			" created_on," +
			" show," +
			" news_date"+
			" from " +
			" news" +
			" limit 0, 50";
			
		}
		
	}

	private static final Logger logger = Logger.getLogger(NewsDao.class);
	
	
	public List<INews> getNews() throws PersistentException {
		SqlParameterSource namedParameters = new MapSqlParameterSource();
		List<INews> newsList = (List<INews>)getNamedParameterJdbcTemplate().query(CONST.QUERY.GET_NEWS, namedParameters, getRowMapper());
		return newsList;
	}

	public INews createNews(INews news) throws PersistentException {
		if( news != null){
			try {
				jdbcInsert = new SimpleJdbcInsert(getJdbcTemplate()).withTableName(getTableName()).usingGeneratedKeyColumns("id");
				jdbcInsert.usingColumns( "id", "title", "description", "created_on", "show", "news_date" );
				
				Map<String, Object> parameters = new HashMap<String, Object>(10);
				parameters.put("title", news.getTitle());
				parameters.put("description", news.getDescription());
				parameters.put("created_on", news.getCreatedOn());
				parameters.put("show_flag", news.getShow());
				
				//FIXME
				parameters.put("news_date",null);
				
				Number id = jdbcInsert.executeAndReturnKey(parameters);
				news.setId(id.longValue());
				return news;
				
			}
			catch (DataAccessException e) {
				throw new PersistentException(e);
			}
		}
		else 
		{
			throw new PersistentException("News Cannot be null");
		}
	}

	public INews updateNews() throws PersistentException {
		return null;
	}

	public INews deleteNews() throws PersistentException {
		return null;
	}

	public INews getNewsByNewsId(Long newsId) throws PersistentException {
		return null;
	}
	
	public List<INews> getNewsByNewsDate(Date date) throws PersistentException  {		
		SqlParameterSource namedParameters = new MapSqlParameterSource();
		List<INews> newsList = (List<INews>)getNamedParameterJdbcTemplate().query(CONST.QUERY.GET_NEWS_BY_NEWS_DATE, namedParameters,
				getRowMapper());
		return newsList;
	}
	
}
